import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/lget/LState.dart';

// class HomePage extends LView<HomeLogic> {
//   HomeLogic logic = Get.put(HomeLogic());
//   @override
//   get body => Scaffold(
//       body: Stack(
//         children: [],
//       ));
// }

abstract class LView<T extends LViewLogic> extends StatefulWidget {
  late T logic;
  String? tag;

  get init => {};

  get body;

  get finish => null;

  @override
  State<StatefulWidget> createState() {
    var state = _state<T>(lInit: () => init, lBody: () => body, lFinish: () => finish);
    if (logic == null) {
      tag = DateTime.now().millisecondsSinceEpoch.toString();
      logic = Get.put(LViewLogic() as T, tag: tag);
    }
    state.logic = logic;
    return state;
  }
}

class LViewLogic extends GetxController {
  BuildContext? context;
}

class _state<T extends LViewLogic> extends LState<LView> {
  T? logic;

  Function lInit;
  Widget Function() lBody;

  Function? lFinish;

  _state({required this.lInit, required this.lBody, required this.lFinish});

  @override
  get body => GetBuilder<T>(builder: (l) {
        logic?.context = context;
        return lBody();
      });

  @override
  get finish {
    if (lFinish != null) {
      lFinish!();
    } else {
      Get.delete<T>(tag: widget.tag);
    }
  }

  @override
  get init => lInit();
}
